home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 297_01 / pralloc.c < prev    next >
C/C++ Source or Header  |  1991-12-30  |  18KB  |  603 lines

  1. /* pralloc.c */
  2. /* allocation */
  3. /* 21/12/91 
  4.    dump_stack renamed dump_ancestors 
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "prtypes.h"
  9.  
  10. #define NDEBUG 1 /* turn off checking */
  11. #define PAIRS_TOGETHER 1 
  12. /* Only pairs are allocated on heap.
  13. * This is useful in the case of 8086 architercture.
  14. * as pairs take up a lot of room.
  15. */
  16. /* define STATISTICS used to see how much each structure takes */
  17. #define DEFAULTSIZE 32000     /* default memory zone size     */
  18. #define BUFFSIZE 1000        /* io buffer sizes        */
  19.  
  20. /* error messages */
  21. #define OVERFLOW "overflow"
  22. #define SUBSTSPACE "substitution stack"
  23. #define DYNSPACE "control stack"
  24. #define TRAILSPACE "trail"
  25. #define HEAPSPACE "heap"
  26. #define STRINGSPACE "string zone"
  27. #define TEMPSPACE "temp"
  28. #define SEESTACK "Stack dump?(y/n)"
  29. #define WILDPOINTER "stray pointer!"
  30.  
  31. /* byte alignment */
  32. #define ALIGN(X) X>>=2,X<<=2,X+=4;
  33.  
  34. /* #define CAN_ALLOC(HOWMUCH, MAXPTR, CURRPTR)  (((MAXPTR)-(CURRPTR)) > HOWMUCH)*/
  35. #define CAN_ALLOC(HOWMUCH, MAXPTR, CURRPTR)  ((CURRPTR + HOWMUCH) < MAXPTR)
  36. /* This macro is used in the following circumstance:
  37.  *  MAXPTR is the top of a zone,
  38.  *  CURRPTR is the current pointer in the zone, it is less than MAXPTR.
  39.  *  HOWMUCH is an integer that says how much you want to increase CURRPTR
  40.  *  but you must stay less than MAXPTR.
  41. */
  42.  
  43. #define ADDRESS_DIFF(PTR1, PTR2) (char *)(PTR1) - (char *)(PTR2)
  44. typedef char *void_ptr_t;
  45.  
  46. void alloc_err(), fatal_alloc_err(), clean_pred();
  47.  
  48. #ifdef STATISTICS
  49. /* These numbers let you monitor how much the different 
  50.   * structures consume.
  51.   * It may suprise you to know that integers and variables 
  52.   * consume nothing because they are stored directly in a node.
  53.   */
  54. zone_size_t Atom_consumption;
  55. zone_size_t Pair_consumption;
  56. zone_size_t Node_consumption;
  57. zone_size_t Clause_consumption;
  58. zone_size_t String_consumption;
  59. zone_size_t Predrec_consumption;
  60. #ifdef REAL
  61. zone_size_t Real_consumption;
  62. #endif
  63. #ifdef CHARACTER
  64. zone_size_t Char_consumption;
  65. #endif
  66. #endif
  67.  
  68.  
  69. char *Read_buffer;        /* read tokens into this */
  70. char *Print_buffer;        /* used by pr_string */
  71. clause_ptr_t Bltn_pseudo_clause; /* see prlush.c  */
  72. node_ptr_t Printing_var_nodeptr; /* see prprint.c */
  73.  
  74. static char *Heap_mem; /* bottom of heap */
  75. static char *Heap_ptr; /* allocate from this and move this up */
  76. static char *HighHeap_ptr; /* top of heap */
  77.  
  78. static char *Str_mem; /* this is used to allocate permanent strings 
  79.             and perhaps other objects if you want to keep the pairs
  80.             together
  81.             */
  82. static char *Str_ptr; /* allocate from this and move this up */
  83. static char *HighStr_ptr; /* top of zone */
  84.  
  85. dyn_ptr_t Dyn_mem; /* bottom of control stack 
  86.            (and zone for those temporary objects that disappear on backtrack,
  87.             although the substitution zone could do as well) */
  88. dyn_ptr_t Dyn_ptr; /* allocate from this and move this up */
  89. dyn_ptr_t HighDyn_ptr; /* top of control stack */
  90.  
  91. node_ptr_t **Trail_mem; /* the trail (used to reset variable bindings) */
  92. node_ptr_t **Trail_ptr; /* like the others */
  93. node_ptr_t **HighTrail_ptr;/* top of zone */
  94.  
  95. subst_ptr_t Subst_mem; /* bottom of (global) variable bindings stack */
  96. subst_ptr_t Subst_ptr; /* allocate from this and move this up */
  97. subst_ptr_t HighSubst_ptr;/* top of zone */
  98.  
  99. temp_ptr_t Temp_mem; /*  For things you might want to 
  100.             create with temp_assert...
  101.             clean with clean_temp */
  102. temp_ptr_t Temp_ptr; /* allocate from this and move this up */
  103. temp_ptr_t HighTemp_ptr;/* top of zone */
  104.  
  105. int    Max_Readbuffer, Max_Printbuffer;
  106.  
  107. atom_ptr_t Nil;
  108. node_ptr_t NilNodeptr;
  109.  
  110. /*******************************************************************************
  111.             ini_alloc()
  112.  Reserve zones and io buffers. 
  113.  ******************************************************************************/
  114. void ini_alloc()
  115. {
  116.     zone_size_t heap_size, str_size, dyn_size, trail_size, subst_size, temp_size;
  117.     extern void_ptr_t os_alloc(); /* see the machine dependent file */
  118.     extern int read_config();   /* see the machine dependent file */
  119.  
  120.     if(!read_config(&heap_size, &str_size, &dyn_size, &trail_size, &subst_size, &temp_size,
  121.         &Max_Readbuffer, &Max_Printbuffer))
  122.     {
  123.         heap_size = DEFAULTSIZE;
  124.         str_size = DEFAULTSIZE;
  125.         dyn_size = DEFAULTSIZE;
  126.         trail_size = DEFAULTSIZE;
  127.         subst_size = DEFAULTSIZE;
  128.         temp_size = DEFAULTSIZE;
  129.         Max_Readbuffer = BUFFSIZE;
  130.         Max_Printbuffer =  BUFFSIZE;
  131.     }
  132.  
  133.     Heap_mem = os_alloc(heap_size);
  134.     Heap_ptr = Heap_mem;
  135.     HighHeap_ptr = Heap_mem + heap_size;
  136.  
  137.     Str_mem = os_alloc(str_size);
  138.     Str_ptr = Str_mem;
  139.     HighStr_ptr = Str_mem + str_size;
  140.  
  141.     Dyn_mem = (dyn_ptr_t)os_alloc(dyn_size);
  142.     Dyn_ptr = Dyn_mem;
  143.     HighDyn_ptr = (dyn_ptr_t)((char *)Dyn_mem + dyn_size);
  144.  
  145.     Trail_mem = (node_ptr_t **)os_alloc(trail_size);
  146.     Trail_ptr = Trail_mem;
  147.     HighTrail_ptr = (node_ptr_t **)((char *)Trail_mem + trail_size);
  148.  
  149.  
  150.     Subst_mem = (subst_ptr_t)os_alloc(subst_size);
  151.     Subst_ptr = Subst_mem;
  152.     HighSubst_ptr = (subst_ptr_t)((char *)Subst_mem + subst_size);
  153.     while(Subst_ptr < HighSubst_ptr)
  154.     {
  155.         Subst_ptr->skel = (node_ptr_t)NULL;
  156.         Subst_ptr++;
  157.     }
  158.  
  159.     Subst_ptr = Subst_mem;
  160.  
  161.     Temp_mem = (temp_ptr_t)os_alloc(temp_size);
  162.     Temp_ptr = Temp_mem;
  163.     HighTemp_ptr = (temp_ptr_t)((char *)Temp_mem + temp_size);
  164.  
  165.     Read_buffer = os_alloc((zone_size_t)Max_Readbuffer);
  166.     Print_buffer = os_alloc((zone_size_t)Max_Printbuffer);
  167. }
  168.  
  169. /*******************************************************************************
  170.             my_Heap_alloc()
  171.  ******************************************************************************/
  172. static void_ptr_t my_Heap_alloc(how_much)
  173. my_alloc_size_t how_much;
  174. {
  175.     void_ptr_t ret;
  176.     ALIGN(how_much);
  177.  
  178.     if(!CAN_ALLOC(how_much ,HighHeap_ptr, Heap_ptr))
  179.     {
  180.         fatal_alloc_err(HEAPSPACE);
  181.     }
  182.     else
  183.         ret = Heap_ptr;
  184.     Heap_ptr += how_much;
  185.     return(ret);
  186. }
  187.  
  188. /*******************************************************************************
  189.             my_Str_alloc()
  190. Allocate on permanent string space. 
  191.  ******************************************************************************/
  192. static void_ptr_t my_Str_alloc(how_much)
  193. my_alloc_size_t how_much;
  194. {
  195.     void_ptr_t ret;
  196.     ALIGN(how_much);
  197.  
  198.     if(!(CAN_ALLOC(how_much, HighStr_ptr, Str_ptr)))
  199.     {
  200.         fatal_alloc_err(STRINGSPACE);
  201.     }
  202.     else
  203.         ret = Str_ptr;
  204.     Str_ptr += how_much;
  205.     return(ret);
  206. }
  207.  
  208. /********************************************************************************
  209.             my_Dyn_alloc()
  210. Allocate on the control stack. 
  211. This is for objects that disappear on backtracking.
  212.  ******************************************************************************/
  213. dyn_ptr_t my_Dyn_alloc(how_much)
  214. my_alloc_size_t how_much; /* in bytes */
  215. {
  216.     dyn_ptr_t ret;
  217.     ALIGN(how_much);
  218.     if(!(CAN_ALLOC(how_much ,HighDyn_ptr, Dyn_ptr)))
  219.     {
  220.         alloc_err(DYNSPACE);
  221.     }
  222.     else
  223.         ret = Dyn_ptr;
  224.     Dyn_ptr += how_much;
  225.     return(ret);
  226. }
  227.  
  228. /*******************************************************************************
  229.             my_Trail_alloc()
  230.  Allocate one trail element.
  231.  ******************************************************************************/
  232. node_ptr_t ** my_Trail_alloc()
  233. {
  234.     node_ptr_t ** ret;
  235.  
  236.     if( Trail_ptr >= HighTrail_ptr)
  237.     {
  238.         alloc_err(TRAILSPACE);
  239.     }
  240.     else
  241.         ret = Trail_ptr;
  242.     Trail_ptr ++;
  243.     return(ret);
  244. }
  245.  
  246. /*******************************************************************************
  247.             my_Subst_alloc()
  248. Allocate how_much bytes on the substitution stack.
  249. This is more speed-efficient than allocating struct substs on an array of 
  250.  structures, as there is no multiplication.
  251.  ******************************************************************************/
  252. subst_ptr_t my_Subst_alloc(how_much)
  253. my_alloc_size_t how_much; /* in bytes */
  254. {
  255.     subst_ptr_t ret;
  256. #ifndef  NDEBUG
  257.     if(how_much % sizeof(struct subst))INTERNAL_ERROR("alignment");
  258. #endif    
  259.     if(! CAN_ALLOC(how_much ,HighSubst_ptr, Subst_ptr))
  260.     {
  261.         alloc_err(SUBSTSPACE);
  262.     }
  263.     else
  264.         ret = Subst_ptr;
  265.     Subst_ptr += how_much;
  266.     return(ret);
  267. }
  268.  
  269. /********************************************************************************
  270.             my_Temp_alloc()
  271. Allocate in temporary memory.
  272. This is for objects that disappear
  273. when y